package com.xueyi.aixihu.utils;

import com.xueyi.aixihu.response.UploadImageResponse;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * 描述:base64转换工具类
 * @author 刘梓江
 * @Date 2021/9/21 5:08 下午
 */
public class Base64DecodedMultipartFile implements MultipartFile{

    private final byte[] imgContent;
    private final String header;

    public Base64DecodedMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    @Override
    public String getName() {
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        return header.split(":")[1];
    }

    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    @Override
    public long getSize() {
        return imgContent.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(imgContent);
    }

    /**
     * 操作base64图片内容
     *
     * @param content
     * @return
     */
    public static String operationBase64ImageInfo(String content) {
        String newInfo = "";
        String base64Info = "";
        int currentIndex = -1;
        while ((currentIndex = content.indexOf("src=")) != -1) {
            //截取获取普通文本
            newInfo += content.substring(0, currentIndex + 5);
            content = content.substring(currentIndex + 5);
            try {
                //获取出对应的base64图片文本信息
                base64Info = content.substring(0, content.indexOf("\""));
                //转换成文件对象并转换成一个http的在线文件请求地址
                MultipartFile multipartFile = Base64DecodedMultipartFile.base64ToMultipart(base64Info);
                base64Info = AliYunOSSUtil.upload(multipartFile, UploadImageResponse.UploadImageStatus.SZ_IV);
            } catch (Exception e) {
            }
            newInfo += base64Info;
            try {
                content = content.substring(content.indexOf("\""));
            } catch (Exception e) {
            }
        }
        newInfo += content;
        return newInfo;
    }

    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStr = base64.split(",");
            byte[] b = new byte[0];
            Base64 decoder = new Base64();
            b = decoder.decodeBase64(baseStr[1]);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new Base64DecodedMultipartFile(b, baseStr[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
